package net.pimpas.web.jsf.renderers.graphicimage.html;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import net.pimpas.web.jsf.components.graphicimage.UIGraphicImage;
import net.pimpas.web.jsf.renderers.RendererBase;
import net.pimpas.web.jsf.util.ComponentUtils;

import org.apache.myfaces.shared_impl.renderkit.html.HTML;
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;

public class HtmlGraphicImageRenderer extends RendererBase {
	
	@Override
	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UIGraphicImage.class)) return;
		super.initResourceManager(context, component);
		
		UIGraphicImage graphicImage = (UIGraphicImage) component;
		ResponseWriter writer = context.getResponseWriter();
		String url =  graphicImage.getUrl();
        writer.startElement(HTML.IMG_ELEM, component);
        HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
        writer.writeURIAttribute(HTML.SRC_ATTR,
                                 url,
                                 null);
        HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
        writer.endElement(org.apache.myfaces.shared_impl.renderkit.html.HTML.IMG_ELEM);
	}
}